package com.gec.hrm.dao.impl;

import com.gec.hrm.pojo.Dept;
import com.gec.hrm.pojo.Job;
import com.gec.hrm.util.JDBCUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class JobDaoImpl  extends JDBCUtils<Job> implements JobDao{
    @Override
    public List<Job> getAllJob() {
        return query("select * from job_inf");
    }
    @Override
    public List<Job> jobList(String name, int page, int limit) {
        String sql = "select * from job_inf where 1=1 ";//万能条件
        //需要对条件做一下判断，不为空
        //再输入某个条件后，另外一个条件不是null,而是一个空的字符串
        if(name !=null && !"".equals(name))
        {
            sql+= "and name like '%"+name+"%'";
        }
        sql+=" limit "+(page-1)*limit +","+limit+"";
        List<Job> job = query(sql);
        return job;
    }

    @Override
    public boolean checkJobName(String name) {
        List<Job> job= query("select * from job_inf where name = ?",name);
        if(job.size()>0){
            return true;
        }
        return false;
    }

    @Override
    public int addJob(Job job) {
        return update("insert into job_inf  values(null,?,?,null)",
                job.getName(), job.getRemark());
    }

    @Override
    public int updJob(Job job) {
        return update("update job_inf set name=?,remark=? where id=?",
                job.getName(),job.getRemark(),job.getId());

    }

    @Override
    public int count() {
        List<Job> jobs= query("select * from job_inf");
        return jobs.size();
    }

    @Override
    public int delJob(int id) {
        return update("delete from job_inf where id=?",id);
    }



    @Override
    public Job getBean(ResultSet rs) {
        Job job=new Job();
        try {
            job.setId(rs.getInt("ID"));
            job.setName(rs.getString("NAME"));
            job.setRemark(rs.getString("REMARK"));
            job.setStatus(rs.getInt("state"));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return job;
    }
}
